package com.sxz.sbs.applibrary.banner;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sxz.sbs.applibrary.R;
/**
 * 
* @ClassName: AutoSlippingViewPagerIndicator 
* @Description: 滚动到第几页
 */
public class AutoSlippingViewPagerIndicator extends LinearLayout {
	public AutoSlippingViewPagerIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public AutoSlippingViewPagerIndicator(Context context) {
		super(context);
		init(context);
	}

	private Context mContext;
	private int indicatorsCount = -1;
	private Drawable indicatorNor;
	private Drawable indicatorSel;

	private void init(Context context) {

		this.mContext = context;
		this.setOrientation(LinearLayout.HORIZONTAL);// 设置水平摆放里面的indicator元素
		indicatorNor = context.getResources().getDrawable(
				R.drawable.icon_indicator_unselete);
		indicatorSel = context.getResources().getDrawable(
				R.drawable.icon_indicator_seleted);
	}

	/**
	 * 设置所有的数量
	 * 
	 * @param indicatorsCount
	 *            需要大于0
	 */
	protected void setIndacatorCount(int indicatorsCount) {

		if (indicatorsCount < 0) {
			return;
		}
		this.indicatorsCount = indicatorsCount;
		iniIndicators(indicatorsCount);// 初始化所有的View
	}

	/**
	 * 获取当前Indicator的数量
	 * */
	public int getIndicatorCount() {
		return this.indicatorsCount;
	}

	/**
	 * 根据indicator的数量初始化indicator对象
	 * 
	 * @param indicatorsCount
	 * */
	private void iniIndicators(int indicatorsCount) {

		// 先移除当前的indicator
		if (this.getChildCount() > 0) {
			this.removeAllViews();
		}
		for (int i = 0; i < indicatorsCount; i++) {

			ImageView ivIndicator = new ImageView(mContext);


			ivIndicator
					.setLayoutParams(new LayoutParams(
							LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT, 1.0f));

			ivIndicator.setImageResource(R.drawable.icon_indicator_unselete);
			this.addView(ivIndicator);
		}
		if(indicatorsCount == 1){
			this.setVisibility(View.GONE);
		}else{
			this.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 设置当前的索引状态
	 * 
	 * @param index
	 * */
	protected void setCurrentPageIndicator(int index) {

		if (indicatorsCount == -1) {
			return;
		} else if (index < 0 || index > indicatorsCount - 1) {
			return;
		}
		refreshIndicator(index);// 对LinearLayout中的View进行刷新
	}

	/**
	 * 对Indicator进行刷新
	 * */
	private void refreshIndicator(int index) {
		int childCount = this.getChildCount();
		for (int i = 0; i < childCount; i++) {
			ImageView ivAd = (ImageView) this.getChildAt(i);
			if (i == index) {
				ivAd.setImageDrawable(indicatorSel);
			} else {
				ivAd.setImageDrawable(indicatorNor);
			}
		}
	}

	/**
	 * 设置Indicator的图标
	 * 
	 * */
	protected void setIndicatorIcon(int indicatorNorRes, int indicatorSelRes) {
		indicatorNor = mContext.getResources().getDrawable(indicatorNorRes);
		indicatorSel = mContext.getResources().getDrawable(indicatorSelRes);
	}

	/**
	 * 设置Indicator的图标
	 * 
	 * */
	protected void setIndicatorIcon(Drawable indicatorNor, Drawable indicatorSel) {
		this.indicatorNor = indicatorNor;
		this.indicatorSel = indicatorSel;
	}

	/**
	 * 设置Indicator之间的距离
	 * */
	protected void setIndicatorPaddings(int padding) {
		int indicatorCount = this.getChildCount();
		for (int i = 0; i < indicatorCount; i++) {
			View view = this.getChildAt(i);
			view.setPadding(padding, 0, padding, 0);
		}
	}
}
